package cn.jiyun.user.service.impl;

import cn.jiyun.common.exception.IncorrectPasswordException;
import cn.jiyun.common.exception.UserNotFoundException;
import cn.jiyun.common.utils.HttpUtils;
import cn.jiyun.model.User;
import cn.jiyun.user.mapper.UserMapper;
import cn.jiyun.user.service.UserSerivce;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserSerivce {
    /**
     * 用户登录。
     *
     * @param user 包含用户名和密码的用户对象。
     * @return 登录成功的用户对象。
     * @throws UserNotFoundException 如果用户名不存在。
     * @throws IncorrectPasswordException 如果密码错误。
     */
    @Autowired
    private HttpUtils httpUtils;
    @Value("${aliyun.message.appcode}")
    private String appcode;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    UserMapper userMapper;
    @Override
    public String login(User user) {
        // 根据用户名查询用户
//
//        String password = user.getPassword();
//        String password1 = DigestUtils.md5DigestAsHex(password.getBytes());
//        userLoginDto.setPassword(password1);
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUsername,user.getUsername());
        userLambdaQueryWrapper.eq(User::getPassword,user.getPassword());
        User userInfo= userMapper.selectOne(userLambdaQueryWrapper);
        if (userInfo==null){
            return "";
        }
        String token = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set("token:"+token, JSON.toJSONString(userInfo));
        return token;

//        String username = user.getUsername();
//        LambdaQueryWrapper<User> eq = new LambdaQueryWrapper<User>().eq(User::getUsername, username);
//        User user1 = this.baseMapper.selectOne(eq);
//
//        // 用户名不存在时抛出异常
//        if (user1==null){
//            throw  new UserNotFoundException("用户名不存在");
//        }
//
//        // 验证密码，正确则返回用户对象
//        if (user1.getPassword().equals(user.getPassword())){
//            return user1;
//        }
//
//        // 密码错误时抛出异常
//        throw new IncorrectPasswordException("密码错误");
    }

    @Override
    public void loginPhone(String phone) {
        String random = RandomStringUtils.randomNumeric(4);
        httpUtils.sendMessage(phone,random,appcode);
        redisTemplate.opsForValue().set("phone:"+phone,random,5, TimeUnit.MINUTES);
    }

    @Override
    public boolean phoneLogin(String phone, String code) {
        String s = redisTemplate.opsForValue().get("phone:" + phone);
        if (s.equals(code)){
            return true;
        }
        return false;
    }

}
